package hms.rooms;

import hms.database.DAO;
import hms.database.DAOnoGUI;

import java.sql.ResultSet;
import java.sql.SQLException;

public class AmenitiesHandler {

	private DAO tempDAO;
	private ResultSet results;
	
	public AmenitiesHandler(){
		tempDAO = DAOnoGUI.getDAO();
	}
	
	private void getAmenitiesFromDatabase(String roomType){
		
		results = tempDAO.getResult("SELECT * FROM INCLUDES_AMENTITIES WHERE ROOMTYPELABEL = '" + roomType + "';");
		
	}
	
	public void removeAmenityFromDatabase(String roomType, String amenity) throws SQLException{
		
		getAmenitiesFromDatabase(roomType);
		
		while(results.next()){

			if(results.getString("AMENITY").equalsIgnoreCase(amenity)){
				tempDAO.updateCommand("DELETE FROM INCLUDES_AMENTITIES WHERE ROOMTYPELABEL = '" + roomType + "' AND AMENITY = '" + amenity + "';");
			}
		}
	}
	
	public void addAmenityToDatabase(String roomType, String amenity) throws SQLException{
		
		getAmenitiesFromDatabase(roomType);
		
		Boolean exists = false;
		
		while(results.next()){
			if(results.getString("AMENITY").equalsIgnoreCase(amenity)){
				exists = true;
			}
		}
		
		if(!exists){
			tempDAO.updateCommand("INSERT INTO INCLUDES_AMENTITIES (ROOMTYPELABEL, AMENITY) VALUES('" + roomType + "', '" + amenity + "');");
		}
	}
	
	public boolean exists(String roomType, String amenity) throws SQLException{
		
		getAmenitiesFromDatabase(roomType);
		
		Boolean exists = false;
		
		while(results.next()){
			if(results.getString("AMENITY").equalsIgnoreCase(amenity)){
				exists = true;
			}
		}
		
		return exists;
	}
	
}
